package com.dhcc.DRGsHosp.facade.impl;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Named;

import org.dayatang.domain.EntityRepository;
import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.QueryChannelService;
import org.dayatang.utils.Page;
import org.openkoala.koala.commons.InvokeResult;
import org.springframework.beans.factory.annotation.Autowired;

import com.dhcc.DRGsHosp.application.MiMonthProPortionApplication;
import com.dhcc.DRGsHosp.core.domain.MiMonthProPortion;
import com.dhcc.DRGsHosp.facade.MiMonthProPortionFacade;
import com.dhcc.DRGsHosp.facade.dto.MiMonthProPortionDTO;
import com.dhcc.DRGsHosp.facade.impl.assembler.MiMonthProPortionAssembler;

/**
 * @title		:MiMonthProPortionFacadeImpl.java
 * @description	:
 * @author		:WangTeng
 * @date		:2016年7月25日
 * @version     :1.0
 * @copyright   :DHCC 2013-2014 
 * 
*/
@Named("drgsHospFacadeImpl31")
public class MiMonthProPortionFacadeImpl implements MiMonthProPortionFacade {

	@Autowired
	private MiMonthProPortionApplication  application;

	private QueryChannelService queryChannel;

    private QueryChannelService getQueryChannelService(){
       if(queryChannel==null){
          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
       }
     return queryChannel;
    }
    
    private EntityRepository repository;
	
	private EntityRepository getRepository() {
		if (repository == null) {
			repository = InstanceFactory.getInstance(
					EntityRepository.class, "repository");
		}
		return repository;
	}
	
	public InvokeResult getMiMonthProPortion(String mimpMonth) {
		return InvokeResult.success(MiMonthProPortionAssembler.toDTO(application.getMiMonthProPortion(mimpMonth)));
	}
	
	public InvokeResult creatMiMonthProPortion(MiMonthProPortionDTO miMonthProPortionDTO) {
		application.creatMiMonthProPortion(MiMonthProPortionAssembler.toEntity(miMonthProPortionDTO));
		return InvokeResult.success();
	}
	
	public InvokeResult updateMiMonthProPortion(MiMonthProPortionDTO miMonthProPortionDTO) {
		application.updateMiMonthProPortion(MiMonthProPortionAssembler.toEntity(miMonthProPortionDTO));
		return InvokeResult.success();
	}
	
	public InvokeResult removeMiMonthProPortion(String mimpMonth) {
		application.removeMiMonthProPortion(application.getMiMonthProPortion(mimpMonth));
		return InvokeResult.success();
	}
	
	public InvokeResult removeMiMonthProPortions(String[] mimpMonths) {
		Set<MiMonthProPortion> miMonthProPortions= new HashSet<MiMonthProPortion>();
		for (String mimpMonth : mimpMonths) {
			miMonthProPortions.add(application.getMiMonthProPortion(mimpMonth));
		}
		application.removeMiMonthProPortions(miMonthProPortions);
		return InvokeResult.success();
	}
	
	public List<MiMonthProPortionDTO> findAllMiMonthProPortion() {
		return MiMonthProPortionAssembler.toDTOs(application.findAllMiMonthProPortion());
	}
	
	public Page<MiMonthProPortionDTO> pageQueryMiMonthProPortion(MiMonthProPortionDTO queryVo, int currentPage, int pageSize) {
		List<Object> conditionVals = new ArrayList<Object>();
	   	StringBuilder jpql = new StringBuilder("select _miMonthProPortion from MiMonthProPortion _miMonthProPortion   where 1=1 ");
	   	if (queryVo.getMimpMonth() != null && !"".equals(queryVo.getMimpMonth())) {
	   		jpql.append(" and _miMonthProPortion.mimpMonth like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getMimpMonth()));
	   	}		
	   	if (queryVo.getMimpValue() != null) {
	   		jpql.append(" and _miMonthProPortion.mimpValue=?");
	   		conditionVals.add(queryVo.getMimpValue());
	   	}	
        Page<MiMonthProPortion> pages = getQueryChannelService()
		   .createJpqlQuery(jpql.toString())
		   .setParameters(conditionVals)
		   .setPage(currentPage, pageSize)
		   .pagedList();
		   
        return new Page<MiMonthProPortionDTO>(pages.getStart(), pages.getResultCount(),pageSize, MiMonthProPortionAssembler.toDTOs(pages.getData()));
	}

	/* (non-Javadoc)
	 * @see com.dhcc.DRGsHosp.facade.MiMonthProPortionFacade#addHCTotalFund(java.lang.String, java.lang.String)
	 */
	@Override
	public InvokeResult addHCTotalFund(String HCTotalFundYear,
			String HCTotalFund) {
		application.addHCTotalFund(HCTotalFundYear, HCTotalFund);
		return InvokeResult.success();
	}

	/**
	 * @methodname :selectMiMonthProByType
	 * @Description : 根据基金类型查询查询基金的所有年份每个月的分配比例
	 * @author:yujiaolong
	 * @return InvokeResult
	 * @date :2017年6月22日
	 */
	@Override
	public InvokeResult selectMiMonthProByType(Map<String, Object> dto) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list = application.selectMiMonthProByType(dto);
		
		return InvokeResult.success(list);
	}

	/**
	 * @methodname :addMonthProporByType
	 * @Description : 保存和更新基金的所有年份每个月的分配比例
	 * @author:yujiaolong
	 * @return InvokeResult
	 * @date :2017年6月23日
	 */
	@Override
	public InvokeResult addMonthProporByType(Map<String, Object> dto) {
		String  jsonStr =dto.get("jsonStr") == null ? "" :dto.get("jsonStr").toString();
		String hospCode = dto.get("hospCode") == null ? "" : dto.get("hospCode").toString();
		jsonStr = jsonStr.replaceAll("\"", "");
		String fundarr[] = jsonStr.split(",");
		System.out.println(fundarr);
		for(String str :fundarr){
			String mimpMonth = str.split(":")[0];
			try {
				Double minmpValue = new Double(str.split(":")[1]);
				String fundType = dto.get("fundType").toString();
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("mimpMonth",mimpMonth);
				map.put("fundType", fundType);
				map.put("minmpValue", minmpValue);
				map.put("hospCode", hospCode);
				application.addMonthProporByType(map);
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return InvokeResult.success();
	}

	
	/**
	 * 
	 * @methodname    :addMonthProportion
	 * @description   :保存医保总额
	 * @param         :
	 * @author        :yujiaolong
	 * @return        :
	 * @date          :2017年6月23日
	 */
	@Override
	public void addHCTotalFundByType(Map<String, Object> dto) {
		
		application.addHCTotalFundByType(dto);
	}
	
	/**
	 * @methodname :getFundByType
	 * @Description : 根据年份获取基金总额
	 * @author:yujiaolong
	 * @return InvokeResult
	 * @date :2017年6月27日
	 */
	@Override
	public InvokeResult getFundByType(Map<String, Object> dto) {
		return InvokeResult.success(application.getFundByType(dto)) ;
	}
	
	
}
